package com.delayqueue.core.thread;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class ThreadHandler {

    @Autowired
    private ThreadConfig threadConfig;

    private ExecutorService executorService;

    public void init() {

        log.info("开始初始化线程池");
        executorService = new ThreadPoolExecutor(threadConfig.getCorePoolSize(), threadConfig.getMaximumPoolSize(), threadConfig.getKeepAliveTime(), TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(threadConfig.getMaximumPoolSize()));
        log.info("线程池初始化完毕");
    }

    public void execute(Runnable runnable) {
        executorService.execute(runnable);
    }


}
